package com.droidathletics.boundary.dao;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class SportsmanDAOAdapter {
	
	private static final String DATABASE_NAME = "droidathleticsdb.db";
	private static final int DATABASE_VERSION = 1;
	
	private static final String PERFORMANCECOMP_TABLE_NAME = "performancecomp";
    private static final String PERFORMANCECOMP_TABLE_CREATE =
        "CREATE TABLE " + PERFORMANCECOMP_TABLE_NAME + " (namesportsman TEXT, " +
		"lastnamesportsman TEXT, namecompetition TEXT, timeone NUMERIC, " +
		"timetwo NUMERIC, timethree NUMERIC, timefour NUMERIC, timefive NUMERIC, " +
		"PRIMARY KEY(namesportsman, lastnamesportsman, namecompetition))";
	
	private static final String MICROCYCLE_TABLE_NAME = "microcycles";
    private static final String MICROCYCLE_TABLE_CREATE =
        "CREATE TABLE " + MICROCYCLE_TABLE_NAME + " (macrocycle TEXT, numberofweek INTEGER, " +
		"period TEXT, stage TEXT, microcycletype TEXT, " +
		"objective TEXT, numberhoursperweek INTEGER, analacvelocidad INTEGER, " +
		"anlacresistveloc INTEGER, anaaerobiomixto INTEGER, aerobico INTEGER, " +
		"fuerzamaxima INTEGER, fuerzarapida INTEGER, fuerzaresistencia INTEGER, " +
		"flexibilidad INTEGER, capcoordinativas INTEGER, tecnica INTEGER, " +
		"tecnicatactica INTEGER, psicologica INTEGER, pbiologica INTEGER, pteorica INTEGER, " +
		"competencias INTEGER, controles INTEGER, PRIMARY KEY(macrocycle, numberofweek))";
	
	private static final String MACROCYCLE_TABLE_NAME = "macrocycles";
    private static final String MACROCYCLE_TABLE_CREATE =
        "CREATE TABLE " + MACROCYCLE_TABLE_NAME + " (name TEXT, startdate TEXT, enddate TEXT, " +
        		"teamname TEXT, branch TEXT, category TEXT, competition TEXT, PRIMARY KEY(name))";
	
	private static final String COMPETITION_TABLE_NAME = "competitions";
    private static final String COMPETITION_TABLE_CREATE =
        "CREATE TABLE " + COMPETITION_TABLE_NAME + " (name TEXT, date TEXT, " +
        		"place TEXT, numbersportsman INTEGER, PRIMARY KEY(name))";
	
	private static final String SPORTSMAN_TABLE_NAME = "sportsmans";
    private static final String SPORTSMAN_TABLE_CREATE =
        "CREATE TABLE " + SPORTSMAN_TABLE_NAME + " (name TEXT, lastname TEXT, " +
        		"height REAL, weight REAL, phonenumber NUMERIC, PRIMARY KEY(name, lastname))";
	
    private SQLiteDatabase database;
	private final Context context;
    private SportsmanDBOpenHelper sportsmanHelper;
    
	public SportsmanDAOAdapter(Context context) {
		this.context = context;
		sportsmanHelper = new SportsmanDBOpenHelper(this.context);
	    this.database = sportsmanHelper.getWritableDatabase();
	}
	
	public static class SportsmanDBOpenHelper extends SQLiteOpenHelper {
		
		public SportsmanDBOpenHelper(Context context) 
		{
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) 
		{
			try {
			db.execSQL(SPORTSMAN_TABLE_CREATE);	
	        } catch (Exception e) {
	            e.printStackTrace();
	        }
	        try {
				db.execSQL(COMPETITION_TABLE_CREATE);
	        } catch (Exception e) {
	            e.printStackTrace();
	        }
	        try {
				db.execSQL(MACROCYCLE_TABLE_CREATE);
	        } catch (Exception e) {
	            e.printStackTrace();
	        }
	        try {
				db.execSQL(MICROCYCLE_TABLE_CREATE);
	        } catch (Exception e) {
	            e.printStackTrace();
	        }
	        try {
				db.execSQL(PERFORMANCECOMP_TABLE_CREATE);
	        } catch (Exception e) {
	            e.printStackTrace();
	        }
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			db.execSQL("DROP TABLE IF EXISTS " + SPORTSMAN_TABLE_NAME);
			onCreate(db);	
		}
	}

    public void close() {
        sportsmanHelper.close();
    }
    
    public void deleteTable()
    {
    	this.database.delete(SPORTSMAN_TABLE_NAME, null, null);
    }
    
    public long createSportsman(String name, String lastname, String height, String weight,
    		String phoneNumber) {
        
    	long result = -1;
    	
    	ContentValues values = new ContentValues();
        values.put("name", name);
        values.put("lastname", lastname);
        values.put("height", Double.parseDouble(height));
        values.put("weight", Double.parseDouble(weight));
        values.put("phonenumber", Long.parseLong(phoneNumber));
        
        try {
            result = database.insertOrThrow(SPORTSMAN_TABLE_NAME, null, values);
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return result;
    }
    
    public long updateSportsman(String oldName, String oldLastName, String name, 
    		String lastname, String height, String weight, String phoneNumber) {
        
    	long result = -1;
    	
    	ContentValues values = new ContentValues();
        values.put("name", name);
        values.put("lastname", lastname);
        values.put("height", Double.parseDouble(height));
        values.put("weight", Double.parseDouble(weight));
        values.put("phonenumber", Long.parseLong(phoneNumber));
        
        try {
            result = database.update(SPORTSMAN_TABLE_NAME, values, 
            		      "name='"+name+"' AND lastname='"+lastname+"'", null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return result;
    }
    
    public long deleteSportsman(String name, String lastName){
        long result = -1;
        
    	try {
            result = database.delete(SPORTSMAN_TABLE_NAME, "name='"+name+"' AND lastname='"+lastName+"'", null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return result;
    	
    }
    
   public Cursor readSportstman(){
	   	   
	   return database.query(SPORTSMAN_TABLE_NAME, new String[] {"name", "lastname", "height", "weight", "phonenumber"}, 
			   null, null, null, null, null);

   }
   
   public Cursor searchSportsman(String pam){
   	   
	   return database.query(SPORTSMAN_TABLE_NAME, new String[] {"name", "lastname", "height", "weight", "phonenumber"}, 
			   "name like " + "'" + pam + "%'" + " or " + "lastname like " + "'" + pam + "%'" ,null, null, null, null);

   }

public Cursor searchSportsman(ArrayList<String> splitName) {
	
	return database.query(SPORTSMAN_TABLE_NAME, new String[] {"name", "lastname", "height", "weight", "phonenumber"}, 
			   "name like " + "'" + splitName.get(0) + "%'" + " and " + "lastname like " + "'" + splitName.get(1) + "%'" ,null, null, null, null);
}

}
